package com.android.videoeditor;

import com.android.videoeditor.service.ApiService;
import com.android.videoeditor.service.ApiServiceListener;

import android.app.Activity;
import android.graphics.Bitmap;
import android.media.videoeditor.MediaItem;
import android.media.videoeditor.MediaProperties;
import android.media.videoeditor.MediaVideoItem;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class VideoEditorServiceActivity extends Activity {
    private static final String TAG = "VideoEditorServiceActivity";
    private static final String RPOJECT_NAME = "test";
    private static final String RPOJECT_PATH = "/mnt/sdcard/test";
    private static final String MEDIA_PATH = "/mnt/sdcard/DCIM/Camera/HD.mp4";
    private static final String EXPORT_FILENAME = "/mnt/sdcard/test/HD_export.mp4";
    
    private static final int THUMBNAIL_NUMS = 10;
    private static final int THUMBNAIL_WIDTH = 156;
    private static final int THUMBNAIL_HEIGHT = 88;
    
    private static final int EXPORT_ASPECT_RATIO = MediaProperties.ASPECT_RATIO_11_9;
    private static final int EXPORT_HEIGHT = MediaProperties.HEIGHT_144;
    private static final int EXPORT_BITRATE = MediaProperties.BITRATE_192K;
    private static final int EXPORT_AUDIO_CODEC = MediaProperties.ACODEC_AMRNB;
    private static final int EXPORT_VIDEO_CODEC = MediaProperties.VCODEC_MPEG4;
    
    private final ServiceListener mServiceListener = new ServiceListener();
    private Toast mToast = null;
    private Button mCancelExportButton = null;
    private long mStartTime = 0;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mCancelExportButton = (Button)findViewById(R.id.cancel_export);
        mCancelExportButton.setVisibility(View.GONE);
        mCancelExportButton.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                ApiService.cancelExportVideoEditor(VideoEditorServiceActivity.this, RPOJECT_PATH, MEDIA_PATH);
                mCancelExportButton.setVisibility(View.GONE);
            }
        });
        
        mToast = Toast.makeText(this, "", Toast.LENGTH_LONG);
        ApiService.createVideoEditor(this, RPOJECT_PATH, RPOJECT_NAME, MEDIA_PATH);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ApiService.deleteProject(this, RPOJECT_PATH);
    }

    @Override
    protected void onResume() {
        super.onResume();
        ApiService.registerListener(mServiceListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        ApiService.unregisterListener(mServiceListener);
    }

    private void showMessage(String message) {
        Log.d(TAG, message);
        mToast.setText(message);
        mToast.show();
    }
    
    private class ServiceListener extends ApiServiceListener {
        @Override
        public void onVideoEditorCreated(String projectPath, List<MediaItem> mediaItems,
                Exception exception) {
            showMessage("onVideoEditorCreated" + ((exception == null) ? "" : " --> ex:" + exception));
            if (exception == null) {
                MediaVideoItem mMediaVideoItem = (MediaVideoItem)mediaItems.get(0);
                int indices[] = new int[THUMBNAIL_NUMS];
                for (int i = 0; i < THUMBNAIL_NUMS; i++) {
                    indices[i] = i;
                }
                ApiService.setAspectRatio(VideoEditorServiceActivity.this, RPOJECT_PATH, EXPORT_ASPECT_RATIO);
                mStartTime = System.currentTimeMillis();
                ApiService.getMediaItemThumbnails(VideoEditorServiceActivity.this, RPOJECT_PATH, 
                    mMediaVideoItem.getId(), THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, 0, 
                    mMediaVideoItem.getDuration(), THUMBNAIL_NUMS, 0, indices);
            }
        }

        @Override
        public void onVideoEditorAspectRatioSet(String projectPath, int aspectRatio,
                Exception exception) {
            showMessage("onVideoEditorAspectRatioSet");
        }

        @Override
        public void onVideoEditorExportProgress(String projectPath, String filename, int progress) {
            showMessage("onVideoEditorExportProgress " + progress + "%");
        }

        @Override
        public void onVideoEditorExportComplete(String projectPath, String filename,
                Exception exception, boolean cancelled) {
            showMessage("onVideoEditorExportComplete " + ((exception == null) ? "" : "--> ex:" + exception));
            mCancelExportButton.setVisibility(View.GONE);
            long diff = System.currentTimeMillis() - mStartTime;
            TextView mExportTextView = (TextView)findViewById(R.id.export_text);
            mExportTextView.setText("Export : " + (diff/1000) + " sec");
            ApiService.deleteProject(VideoEditorServiceActivity.this, RPOJECT_PATH);
        }

        @Override
        public void onVideoEditorExportCanceled(String projectPath, String filename) {
            showMessage("onVideoEditorExportCanceled");
            mCancelExportButton.setVisibility(View.GONE);
        }

        @Override
        public void onVideoEditorDeleted(String projectPath, Exception exception) {
            showMessage("onVideoEditorDeleted " + ((exception == null) ? "" : "--> ex:" + exception));
        }

        @Override
        public boolean onMediaItemThumbnail(String projectPath, String mediaItemId,
                Bitmap thumbnail, int index, int token, Exception exception) {
            showMessage("onMediaItemThumbnail " + ((exception == null) ? (index+1) + "/" + THUMBNAIL_NUMS : "--> ex:" + exception)); 
            if (exception != null || index == (THUMBNAIL_NUMS - 1)) {
                long diff = System.currentTimeMillis() - mStartTime;
                TextView mThumbnailTextView = (TextView)findViewById(R.id.thumbnail_text);
                mThumbnailTextView.setText("Thumbnail : " + (diff/1000) + " sec");
                mStartTime = System.currentTimeMillis();
                ApiService.exportVideoEditor(VideoEditorServiceActivity.this, RPOJECT_PATH, EXPORT_FILENAME, 
                    EXPORT_HEIGHT, EXPORT_BITRATE, EXPORT_AUDIO_CODEC, EXPORT_VIDEO_CODEC);
                mCancelExportButton.setVisibility(View.VISIBLE);
            }
            return false;
        }
    }
}